package com.zengqingfa.designpattern.create.singleton;

/**
 *
 * @fileName: Singleton4
 * @author: zengqf3
 * @date: 2021-4-5 21:00
 * @description: 静态延迟初始化实现单例（懒汉式）双检锁/双重校验锁
 */
public class Singleton4 {

    /**
     * 构造函数私有
     */
    private Singleton4() {
    }

    public static volatile Singleton4 instance;

    //获取实例

    public static Singleton4 getInstance() {
        if (instance == null) {
            synchronized (Singleton4.class) {
                if (instance == null) {
                    instance = new Singleton4();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        Singleton4 instance1 = Singleton4.getInstance();
        Singleton4 instance2 = Singleton4.getInstance();
        //com.zengqingfa.designpattern.create.singleton.Singleton4@58372a00
        System.out.println(instance1);
        //ture
        System.out.println(instance2 == instance1);
    }
}
